function batchUpload({ url, files, name, formData, success, fail }) {
  const results = [];
  let count = 0;
  let hasError = false;

  files.forEach((filePath, idx) => {
    wx.uploadFile({
      url,
      filePath,
      name,
      formData,
      success: res => {
        try {
          const data = JSON.parse(res.data);
          if (data.code === 200 && Array.isArray(data.data)) {
            results[idx] = data.data[0];
          } else {
            hasError = true;
          }
        } catch (e) {
          hasError = true;
        }
        count++;
        if (count === files.length) {
          if (hasError) {
            fail && fail();
          } else {
            success && success({ data: results });
          }
        }
      },
      fail: () => {
        hasError = true;
        count++;
        if (count === files.length) {
          fail && fail();
        }
      }
    });
  });
}

module.exports = batchUpload; 